<?php
namespace App\Http\Controllers\Api\v1;

use App\Http\Controllers\Controller;
use App\Libs\wechatRedpack\wechatRedpack;
use Illuminate\Http\Request;
/*
 * 微信红包操作接口
 *  */
class WechatRedpackController extends Controller
{
    /*
     * 发送红包
     * @param $openid 用户的openid
     * @param $amount 发放金额，单位分 100分=1元
     * method：POST
     * return array
     * author Wayne
     * 2017-11-10
     * */
    public function send(Request $request)
    {
        $amount = $request->input('amount');
        $openid = $request->input('openid');
        if(intval($amount) < 100){
            $result = array(
                'errcode'=> 60003,
                'errmsg'=>'金额参数单位为分，金额需大于1元'
            );
            return json_encode($result);
            exit;
        }
        if(is_null($openid)){
            $result = array(
                'errcode'=> 40034,
                'errmsg'=>'参数缺失'
            );
            return json_encode($result);
            exit;
        }
        $datas = config("hsy.wechatRedpack");
        $checkDatas = $this->_checkDatas($datas);
        if(!is_null($checkDatas)){
            return $checkDatas;
            exit;
        }
        // 实例化红包类
        $wechatRedpack = new wechatRedpack($datas);
        // 发送红包
        $result = $wechatRedpack->sendRedpack($openid, $amount);
        return $result;
    }

    /*
     * 检测参数数据是否合格*/
    private function _checkDatas($datas)
    {
        $result = array(
            'errcode'=> 40034,
            'errmsg'=>'参数缺失'
        );
        $keys = ['appId', 'appSecret', 'token', 'mch_id', 'wxappid', 'nick_name', 'send_name', 'client_ip', 'apikey', 'wishing', 'act_name', 'remark', 'amount'];
        foreach($keys as $k=>$v){
            if(!array_key_exists($v,$datas)){
                return json_encode($result);
                exit;
            }
        }
    }
    
    
    
    
    
}